#!/bin/bash

set -e

# build MININIM
if [ -f Makefile ]; then make clean; fi
mv build-aux/texinfo.tex{,.bkp}
autoreconf --install --force
mv build-aux/texinfo.tex{.bkp,}
./configure
make -j4

# build documentation
cd doc
make mininim.pdf
cd ..

# determine version and release directory
if [ "$(arch)" = "i686" ]
then triplet=i386-linux-gnu; bits=32;
elif [ "$(arch)" = "x86_64" ]
then triplet=x86_64-linux-gnu; bits=64;
else exit 1
fi

VERSION=$(grep -Po '#define VERSION "\K.*?(?=")' config.h)-gnu-linux$bits
RELEASE_DIR=release/mininim-$VERSION

# create directory structure
rm -rf $RELEASE_DIR
mkdir -p $RELEASE_DIR/lib

# function to output library dependencies of executables
function libdep () {
    ldd "$@" | grep -o '\W/[^ ]*' | sort -u
}

# copy MININIM executable
cp mininim $RELEASE_DIR/lib/mininim-exec
cp $(libdep mininim) $RELEASE_DIR/lib

# rename loader
mv $RELEASE_DIR/lib/{ld-*,mininim}

# copy dri drivers
cp -r /usr/lib/$triplet/dri $RELEASE_DIR/lib
cp $(libdep $RELEASE_DIR/lib/dri/*.so) $RELEASE_DIR/lib

# copy udev
cp /lib/$triplet/libudev.so.1 $RELEASE_DIR/lib
cp $(libdep $RELEASE_DIR/lib/libudev.so.1) $RELEASE_DIR/lib

# Replace VirtualBox modules by libGL
rm -f $RELEASE_DIR/lib/VBox*
cp /usr/lib/$triplet/libGL.so.1 $RELEASE_DIR/lib
cp $(libdep $RELEASE_DIR/lib/libGL.so.1) $RELEASE_DIR/lib

# copy gdk pixbuf
cp -r /usr/lib/$triplet/gdk-pixbuf-2.0 $RELEASE_DIR/lib
cp $(libdep $RELEASE_DIR/lib/gdk-pixbuf-2.0/2.10.0/loaders/*.so) $RELEASE_DIR/lib

# generate gdk pixbuf cache
cd $RELEASE_DIR
export GDK_PIXBUF_MODULEDIR="lib/gdk-pixbuf-2.0/2.10.0/loaders"
export GDK_PIXBUF_MODULE_FILE="lib/gdk-pixbuf-2.0/2.10.0/loaders.cache"
lib/gdk-pixbuf-2.0/gdk-pixbuf-query-loaders > $GDK_PIXBUF_MODULE_FILE
cd -

# copy gtk pixmap module
mkdir -p $RELEASE_DIR/lib/gtk-2.0/2.10.0/engines
cp /usr/lib/$triplet/gtk-2.0/2.10.0/engines/lib{pixmap,adwaita,murrine}.so \
   $RELEASE_DIR/lib/gtk-2.0/2.10.0/engines
cp $(libdep $RELEASE_DIR/lib/gtk-2.0/2.10.0/engines/*.so) $RELEASE_DIR/lib

# copy init script
tee $RELEASE_DIR/mininim <<'EOF' > /dev/null
#!/bin/bash
app_path="$(dirname $(readlink -f $0))"
export GDK_PIXBUF_MODULEDIR="${app_path}/lib/gdk-pixbuf-2.0/2.10.0/loaders"
export GDK_PIXBUF_MODULE_FILE="${app_path}/lib/gdk-pixbuf-2.0/2.10.0/loaders.cache"
export GDK_NATIVE_WINDOWS=1
export GTK_PATH="${app_path}/lib/gtk-2.0/2.10.0/engines"
export GTK_EXE_PREFIX="${app_path}"
export GTK_THEME=Adwaita
unset GDK_RENDERING GDK_BACKEND
unset GTK_MODULES GTK2_MODULES GTK3_MODULES
unset GTK_IM_MODULE GTK_IM_MODULE_FILE
unset GTK_RC_FILES GTK2_RC_FILES
export LIBGL_DRIVERS_PATH="${app_path}/lib/dri"
"$app_path/lib/mininim" --library-path "$app_path/lib" "$app_path/lib/mininim-exec" "$@"
EOF

# make it executable
chmod +x $RELEASE_DIR/mininim

# copy data
cp -r data $RELEASE_DIR

# copy documentation
cp doc/mininim.pdf $RELEASE_DIR

# copy Allegro configuration
cp allegro5.cfg $RELEASE_DIR/lib

# strip binaries
strip $RELEASE_DIR/lib/mininim-exec
strip $(find -name '*.so*')

# create package
cd release
tar -cf - ${RELEASE_DIR#release/} | xz -9e - > ${RELEASE_DIR#release/}.tar.xz
cd ..

# remove temporary release directory
rm -rf $RELEASE_DIR
